package whu.o2o;


import whu.o2o.View.LoadDialog;
import whu.o2o.util.ConstantData;
import whu.o2o.util.Utils;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.BaseColumns;
import android.provider.MediaStore.Images.Media;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.TextView;
import android.widget.Toast;

import whu.o2o.R;



public abstract class BaseActivity extends Activity {
	protected MyApplication mApplication;
	protected Activity mContext;
	protected int mListViewLastItem;
	protected boolean mFirstPage;
	protected static final int FIRST_PAGE = 1;

	protected Handler mHttpListHandler = new Handler(new Handler.Callback() {

		@Override
		public boolean handleMessage(Message msg) {
			switch (msg.what) {
			case ConstantData.Data.GET_HTTP_RESULT_MSG_CODE:
				if (msg.obj != null && msg.obj instanceof String) {
					String result = (String) msg.obj;
					if (!TextUtils.isEmpty(result)) {
						parseData(result);
						return false;
					}
				}

				showErrorToast();
				break;

			case ConstantData.Data.PARSE_XML_MSG_CODE:
				setAdapter();
				break;
			}

			LoadDialog.closeDialog();
			return false;
		}
	});

	protected Handler mHttpHandler = new Handler(new Handler.Callback() {

		@Override
		public boolean handleMessage(Message msg) {
			switch (msg.what) {
			case ConstantData.Data.GET_HTTP_RESULT_MSG_CODE:
				if (msg.obj != null && msg.obj instanceof String) {
					String result = (String) msg.obj;
					if (!TextUtils.isEmpty(result)) {
						parseData(result);
						return false;
					}
				}

				showErrorToast();
				break;

			case ConstantData.Data.PARSE_XML_MSG_CODE:
				setData();
				break;
			}

			LoadDialog.closeDialog();
			return false;
		}
	});

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mApplication = (MyApplication) getApplication();
		mContext = this;
	}

	@Override
	public void finish() {
		super.finish();
		Utils.hideIme(this);
	}

	protected void showErrorToast() {
		if (!isFinishing()) {
			Toast.makeText(this, "网络连接超时", Toast.LENGTH_SHORT).show();
		}
	}

	protected void parseData(final String xml) {
	}

	protected void setData() {
	}

	protected void setAdapter() {
	}

	protected void setTitle(String text) {
		if (TextUtils.isEmpty(text)) {
			return;
		}

		View view = findViewById(R.id.title);
		if (view != null && view instanceof TextView) {
			((TextView) view).setText(text);
		}
	}

	protected void backBtnClick() {
		View view = findViewById(R.id.back);
		if (view != null) {
			view.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					finish();
				}
			});
		}

	}

	protected void scrollTop(final AbsListView view) {
		view.post(new Runnable() {

			@Override
			public void run() {
				view.setSelection(0);
			}
		});
	}

	/**
	 * 检测容器中是否已经包含此view
	 * 
	 * @param id
	 *            此view的id
	 * @return
	 */
	protected boolean isContainView(ViewGroup container, int id) {
		if (container.getChildCount() > 0) {
			View view = container.getChildAt(0);
			if (view != null && id == view.getId()) {
				return true;
			}
		}

		return false;
	}

	protected void setTextViewText(int resId, CharSequence text) {
		if (!TextUtils.isEmpty(text)) {
			((TextView) findViewById(resId)).setText(text);
		}
	}

	protected void setTextViewText(TextView textView, CharSequence text) {
		if (!TextUtils.isEmpty(text)) {
			textView.setText(text);
		}
	}

	protected String getEditTextString(TextView et) {
		return et.getText().toString().trim();
	}

	protected void deleteCameraUri(Uri uri) {
		Cursor cursor = null;
		try {
			cursor = getContentResolver().query(uri, null, null, null, null);
			if (cursor != null && cursor.moveToFirst()) {
				getContentResolver().delete(Media.EXTERNAL_CONTENT_URI,
						BaseColumns._ID + "='" + cursor.getString(0) + "'", null);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cursor != null) {
				try {
					cursor.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 剪裁图片
	 */
	protected void cropImage(Uri uri, int requestCode, int width, int height) {
		try {
			Intent intent = new Intent("com.android.camera.action.CROP");
			intent.setDataAndType(uri, "image/*");
			intent.putExtra("crop", "true");
			intent.putExtra("aspectX", 1);
			intent.putExtra("aspectY", 1);
			intent.putExtra("outputX", width);
			intent.putExtra("outputY", height);
			intent.putExtra("return-data", true);
			startActivityForResult(intent, requestCode);
		} catch (ActivityNotFoundException anfe) {
			Toast.makeText(mContext, "图片获取失败", Toast.LENGTH_SHORT).show();
		}
	}

	protected boolean isLogin() {
		if (mApplication.getUserId() == 0) {
			Intent intent = new Intent(mContext, LoginActivity.class);
			startActivity(intent);
			Toast.makeText(mContext, "请登录后再操作", Toast.LENGTH_SHORT).show();
			return false;
		}
		return true;
	}
}
